package com.adminx.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.adminx.entity.Account;
import com.adminx.pojo.AccountPojo;
import com.adminx.pojo.NavigationMenuPojo;
import com.adminx.pojo.RolePojo;
import com.adminx.service.AccountService;
import com.adminx.service.ResourceService;
import com.adminx.service.RoleService;
import com.adminx.util.Constant;
import com.adminx.util.PageResults;

@Controller
@RequestMapping("account")
public class AccountController {
	
	@Autowired
	private AccountService accountService;
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private ResourceService resourceService;
	
	/**
	 * 初始化页面
	 */
	@RequestMapping(value = "/init", method = RequestMethod.GET) 
	public String init(ModelMap map){
		NavigationMenuPojo navigationMenuPojo = this.resourceService.queryNavigation("/account/init.html");
		map.put("navigationMenuPojo", navigationMenuPojo);
		map.put("url", "/account/init.html");
		return "admin/account/account";
	}
	
	/**
	 * 分页查询账号
	 * @param map
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/query", method = RequestMethod.GET) 
	public Map<String, Object> query(Integer pageIndex, Integer pageSize, String searchText){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			PageResults<AccountPojo> result = this.accountService.query(pageIndex, pageSize, searchText);
			map.put("result", result);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
	
	/**
	 * 分页查询账号（用于消息管理模块 择推送账号）
	 * @param map
	 * @return
	 */
	/*@RequestMapping(value = "/query2", method = RequestMethod.GET) */
	/*public String query2(ModelMap map,Integer pageIndex, Integer pageSize){
		try {
			PageResults<Account> pageResults = this.accountService.query(pageIndex,pageSize);
			map.put("pageResults", pageResults);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "admin/message/message_account";
	}*/
	
	/**
	 * 查询账号详细信息
	 * @param accountId
	 * @return Account
	 */
	@RequestMapping(value = "/info") 
	@ResponseBody
	public Map<String, Object> info(Long id){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Account account = this.accountService.info(id);
			map.put("account", account);
			map.put("operateMsg", Constant.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
	
	/**
	 * 查询账号关联角色
	 * @param accountId
	 * @return Account
	 */
	@RequestMapping(value = "/roleInfo") 
	@ResponseBody
	public Map<String, Object> roleInfo(Long accountId){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<RolePojo> rolePojos = this.roleService.findRoleListByAccount(accountId);
			map.put("rolePojos", rolePojos);
			map.put("operateMsg", Constant.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
	
	/**
	 * 批量删除
	 * @param accountId
	 * @return
	 */
	@RequestMapping(value = "/delete") 
	@ResponseBody
	public Map<String, Object> delete(String ids){
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			int delNum = this.accountService.delBatch(ids);
			map.put("delNum", delNum);
			map.put("operateMsg", Constant.SUCCESS_CODE);
		}catch(Exception e){
			e.printStackTrace();
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
	
	/**
	 * 保存或新增账�?
	 * @param account
	 * @return 
	 */
	@ResponseBody
	@RequestMapping(value = "/save", method = RequestMethod.POST) 
	public Map<String, Object> save(Account account){
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			this.accountService.save(account);
			map.put("operateMsg", Constant.SUCCESS_CODE);
		}catch(Exception e){
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
	
	/**
	 * 分配角色
	 * @param account
	 * @return 
	 */
	@RequestMapping(value = "/grantRole") 
	@ResponseBody
	public Map<String, Object> grantRole(Long accountId,Long roleId){
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			this.accountService.grantRole(accountId,roleId);
			map.put("operateMsg", Constant.SUCCESS_CODE);
		}catch(Exception e){
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
}
